import {ApiProperty} from "@nestjs/swagger";
import {IsDefined, IsNumber, Length} from "class-validator";
import {IsFile} from "~/modules/tools/upload/file.constraint";
import {MultipartFile} from "@fastify/multipart";
import {Type} from "class-transformer";

export class GoodsSaveDto {

  @ApiProperty({type: 'string', format: 'binary', description: '图片', required: true})
  @IsDefined()
  @IsFile(
    {
      mimetypes: [
        'image/png',
        'image/gif',
        'image/jpeg',
        'image/webp',
        'image/svg+xml',
      ],
      fileSize: 1024 * 1024 * 10,
    },
    {
      message: '文件类型不正确',
    },
  )
  file: MultipartFile

  @ApiProperty({description: '商品名称', type: String, required: true})
  @Length(0, 50, { message: '商品名称长度必须在0到50个字符之间' })
  @IsDefined()
  name: string

  @ApiProperty({description: '商品价格', type: Number, required: true})
  @Type(() => Number)
  @IsNumber({}, { message: '价格必须为数字' })
  @IsDefined()
  price: number

  @ApiProperty({description: '口令', type: String, required: true})
  // 限制字符串长度
  @Length(0, 200, { message: '密码长度必须在0到20个字符之间' })
  @IsDefined()
  password: string
}

export class GoodsDeleteDto {
  @ApiProperty({description: '商品ID', type: Number, required: true})
  @IsDefined()
  @Type(() => Number)
  @IsNumber({}, { message: '商品ID必须为数字' })
  id: number
}
